<template lang="pug">
  .clearfix
    Modal(
      v-model="isShowExportProjectModal"
      title="选择导出项目"
      class-name="vertical-center-modal"
      width="480"
    )
      p.text-center.text-error.mb-30 温馨提示：不选择项目的时候为默认导出全部！
      Form.has-rules-item2(ref="formData" :model="formData" :rules="rules" :label-width="110")
        FormItem.form-item(v-for="(item, index) of formItem" :label="item.label" :key="index" :prop="item.prop")
          template(v-if="item.type === 'select'")
            Select(v-model="formData[item.model]" :placeholder="item.placeholder" label-in-value @on-change="projectChange" clearable)
              Option(v-for="(c_item, c_index) of arrListDown[item.options]" :key="c_index" :value="c_item.value") {{c_item.label}}
      .clearfix(slot="footer")
        Button(type="text" @click="cancel") 取消
        Button(type="primary" @click="handleSubmit") 确定
</template>

<script>
import moment from 'moment'
import { Common } from '@/utils/common.js'
import { MergeBillApi } from '@/api/MergeBill.api.js'
import config from '@/config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro.replace('/api', '/')

export default {
  name: 'ExportProjectModal',

  watch: {
  },

  data () {
    return {
      isShowExportProjectModal: false,
      formData: {
        // aaa: '',
        project: null,
      },
      formItem: [
        // { type: 'radio', label: '项目：', placeholder: '请选择项目', prop: 'aaa', model: 'aaa', radioArr: [{ value: '1', label: '全部' }, { value: '0', label: '部分项目' }] },
        { type: 'select', label: '部分项目：', placeholder: '请选择部分项目', prop: 'project', model: 'project', options: 'BillMergeProject' },
      ],
      rules: {
        // project: [
        //   { required: true, message: '请选择部分项目' }
        // ]
      },
      arrListDown: {
        BillMergeProject: [],
      },
      selectedProject: null,
      companyId: null,
      beginTime: null,
      endTime: null,
      isCanSubmit: true,
    }
  },

  mounted () {
    this.getEnum()
  },

  methods: {
    // 数据字典
    getEnum () {
      let typeName = 'BillMergeProject'
      Common.getSelectValueList(typeName, this.arrListDown)
    },

    // 监听所有调用的方法
    listenCall (methodsWords, param) {
      this[methodsWords](param)
    },

    cancel () {
      this.isShowExportProjectModal = false
    },

    show () {
      this.$refs.formData.resetFields()
      this.isCanSubmit = true
      this.isShowExportProjectModal = true
    },

    projectChange (value) {
      console.log('value: ', value)
      this.selectedProject = value
    },

    async handleSubmit () {
      var params =  {
        companyIds: this.companyIds.join(','),
        beginTime: this.beginTime,
        endTime: this.endTime,
        items: this.selectedProject ? [{value: this.selectedProject.value, text: this.selectedProject.label}] : null
      }
      console.log('params： ', params)
      let res = await MergeBillApi.exportCustomerSummaryDetail(params)
      if (res.status === 200 && res.data.success) {
        console.log('❤ 返回链接: ', baseUrl + res.data.data)
        window.open(baseUrl + res.data.data, '_blank')
        this.isShowExportProjectModal = false
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
